package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理接口")
public class Setmealcontroller {
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据 id来查找套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id来查找套餐")
    public Result<SetmealVO> SelectByid(@PathVariable Long id){
        log.info("根据id来查找套餐{}:",id);
       SetmealVO setmealVO =setmealService.SelectById(id);
       return Result.success(setmealVO);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping("")
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmeal_",key = "#setmealDTO.categoryId")
    public Result sava(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐{}:",setmealDTO);
        setmealService.sava(setmealDTO);
        return Result.success();
    }

    /**
     * 根据Id批量删除
     * @param id
     * @return
     */
    @DeleteMapping("")
    @ApiOperation("根据ID来批量删除")
    @CacheEvict(cacheNames = "setmeal_",allEntries = true)
    public Result DeleteById(@RequestParam List<Long> id){
        log.info("根据iD来批量删除{}:",id);
        setmealService.DeleteById(id);
        return Result.success();
    }

    /**
     * 修改状态
     * @param status
     * @return
     */
    @ApiOperation("修改状态")
@PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal_",allEntries = true)
    public Result modifyStatus(@PathVariable Integer status,Long id){
        log.info("修改套餐状态{}:{}",status,id);
        setmealService.modifyStatus(status,id);
         return Result.success();
    }

    /**
     * 分页查询
     * @param queryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> pagelist(SetmealPageQueryDTO queryDTO){
    log.info("分页查询{}:",queryDTO);
    PageResult result=setmealService.pagelist(queryDTO);
    return Result.success(result);
    }

@PutMapping("")
@ApiOperation("修改套餐信息")
@CacheEvict(cacheNames = "setmeal_",allEntries = true)
    public Result modifysetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息{}:",setmealDTO);
        setmealService.modifysetmeal(setmealDTO);
      return Result.success();
    }
}
